package com.yanzhenjie.andserver;

import com.yanzhenjie.andserver.util.NetUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventManager;
import ohos.event.notification.NotificationRequest;
import ohos.rpc.IRemoteObject;
import ohos.rpc.RemoteException;

import java.net.InetAddress;
import java.util.concurrent.TimeUnit;

public class ServiceAbility extends Ability {

    private String action = "com.yanzhenjie.andserver.hos";
    private Server mServer;
    private Intent mIntent;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        mIntent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction(action)
                .build();
        mIntent.setOperation(operation);
        mServer = AndServer.webServer(this)
                .port(8080)
                .timeout(10, TimeUnit.SECONDS)
                .listener(new Server.ServerListener() {
                    @Override
                    public void onStarted() {
                        InetAddress address = NetUtils.getLocalIPAddress();
                        String ip = address.getHostAddress();

                        mIntent.setParam("andserver",1);
                        mIntent.setParam("ip",ip);
                        CommonEventData eventData = new CommonEventData(mIntent);
                        try {
                            CommonEventManager.publishCommonEvent(eventData);
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onStopped() {
                        mIntent.setParam("andserver",0);
                        CommonEventData eventData = new CommonEventData(mIntent);
                        try {
                            CommonEventManager.publishCommonEvent(eventData);
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onException(Exception e) {
                        e.printStackTrace();
                        mIntent.setParam("andserver",2);
                        CommonEventData eventData = new CommonEventData(mIntent);
                        try {
                            CommonEventManager.publishCommonEvent(eventData);
                        } catch (RemoteException re) {
                            re.printStackTrace();
                        }
                    }
                })
                .build();

        // 创建通知，其中1005为notificationId
        NotificationRequest request = new NotificationRequest(1005);
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle("AndServer").setText("服务运行中...");
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        request.setContent(notificationContent);
//
//        // 绑定通知，1005为创建通知时传入的notificationId
        keepBackgroundRunning(1005, request);

    }

    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
        super.onCommand(intent, restart, startId);
        startServer();
    }

    @Override
    public IRemoteObject onConnect(Intent intent) {
        super.onConnect(intent);
        return null;
    }

    @Override
    public void onDisconnect(Intent intent) {
        super.onDisconnect(intent);
    }

    @Override
    public void onStop() {
        stopServer();
        super.onStop();
    }

    /**
     * Start server.
     */
    private void startServer() {
        mServer.startup();
    }

    /**
     * Stop server.
     */
    private void stopServer() {
        mServer.shutdown();
        cancelBackgroundRunning();
    }
}
